package com.moon.game.command.guest.lair;

import java.util.List;

import com.moon.game.command.BaseCmd;
import com.moon.game.command.RequestSession;
import com.moon.game.constants.Constants;
import com.moon.game.constants.ErrorCode;
import com.moon.game.exception.GException;
import com.moon.game.manager.AnimalMG;
import com.moon.game.resource.model.Animal;
import com.moon.game.service.LairService;
import com.moon.game.service.PlayerService;
import com.moon.game.util.GDateUtils;
import com.moon.game.vos.ErrorMsg;
import com.moon.game.vos.PlayerVo;
import com.moon.game.vos.PlayerVo.Tower;

/**
 * @description 召唤珍兽
 * @author Ranger 作者 E-mail:haozi000005@163.com
 * @version 创建时间：2012-7-14 下午11:22:14
 */
public class CallAnimalCmd extends BaseCmd {

	@Override
	protected void runOnce(RequestSession req) throws GException {
		switch (req.getStatus()) {
			case STATU_INIT:
				doInit(req);
				break;
			case STATU_CHECK:
				doCheckSelf(req);
				break;
			case STATU_RETURN:
				doResponse(req);
				break;
			default:
				break;
		}
	}

	@Override
	protected void doResponse(RequestSession req) throws GException {
		String[] ps = req.getClientRequest().getPs();
		if (ps == null) {
			doResponsePsError(req);
			return;
		}
		PlayerVo player = req.getPlayer();
		List<Tower> towers = player.getTowerList();
		if (towers == null) {
			doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_NO_TOWER, "进入孵化塔出错，你当前没有孵化塔！"));
			return;
		}
		Integer towerId = Integer.parseInt(ps[0]);
		if (towerId == null) {
			doResponsePsError(req);
			return;
		}
		boolean hasTheLair = false;
		Tower theTower = null;
		for (Tower tower : towers) {
			if (tower.getTowerId() == towerId.intValue()) {
				tower.setLastShowTime(GDateUtils.now());
				hasTheLair = true;
				theTower = tower;
				break;
			}
		}
		//没有指定孵化塔
		if (!hasTheLair) {
			doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_NO_TOWER, "你当前没有指定的孵化塔！"));
			return;
		}
		
		Animal oldAnimal = AnimalMG.instance.getAnimal(theTower.getAnimalId());
		req.setVO("animal_hierarchy", oldAnimal.getHierarchy());
		req.setVO("tower_id", theTower.getTowerId());
		//孵化塔正在出租，提示是否继续刷
		if (theTower.getStartRentTime() + theTower.getRentTime() > GDateUtils.now()) {
			if (ps.length < 2 || Integer.parseInt(ps[2]) != 1) {
				req.setVO("is_renting", true);
				response(req, req.getClientResponse());
				return;
			}else {
				//确定取消
				LairService.cancelTheRentingTower(theTower);
			}
		}
		
		boolean needAck = false;
		if (oldAnimal.getHierarchy() == 2 || oldAnimal.getHierarchy() == 1){
			//是神兽，返回是否继续刷的确认表示
			if (ps.length == 1) {
				String info = createInfo(oldAnimal.getHierarchy());
				req.setVO(TAG_INFO, info);
				needAck = true;
			}else {
				Integer select = Integer.parseInt(ps[1]);
				if (select == null) {
					doResponsePsError(req);
					return;
				}
				
				if (select.intValue() == 0) {
					//返回孵化塔
					callOtherCmd("TA_EL", req, req.getClientResponse().getData(), ps);
					return;
				}else if (select.intValue() != oldAnimal.getHierarchy()) {
					String info = createInfo(oldAnimal.getHierarchy());
					req.setVO(TAG_INFO, info);
					needAck = true;
				}
			}
		}
		if (!needAck) {
			//不需要确认的时候扣除金币，召唤珍兽
			Boolean minusStatu = PlayerService.minusGold(req.getSid(), req.getPlayerId(), Constants.CALL_ANIMAL_PRICE);
			if (!minusStatu) {
				doResponseErrorMsg(req,ErrorMsg.build(ErrorCode.ERR_NO_GOLD, "召唤珍兽需要金币" + Constants.CALL_ANIMAL_PRICE + ",你的金币不足！"));
				return;
			}
			PlayerService.doUpdatePlayer(req.getSid(), req.getPlayerId(), player);
			Animal animal = LairService.RefreashAnimal(theTower);
			req.setVO("animal", animal);
		}
		
		req.setVO("incubation_price", Constants.INCUBATION_PRICE);
		req.setVO("destory_price", Constants.DESTORY_LAIR_PRICE);
		req.setVO("call_price", Constants.CALL_ANIMAL_PRICE);
		response(req, req.getClientResponse());
	}
	
	
	private String createInfo(int hierarchy) {
		String info = "";
		if (hierarchy == 1) {
			info = "您的孵化塔内出现比较罕见仙兽，真的要赶走它，重新召唤吗？";
		}else if (hierarchy == 2) {
			info = "您的孵化塔内出现十分罕见神兽，真的要赶走它，重新召唤吗？";
		}
		return info;
	}
}
